package gui;
/**
 * Draws the UI elements for the registration menu
 */
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import model.user.User.Gender;
import model.user.UserSettings;
import controller.RegistrationController;

public class RegistrationDisplay extends Display {
	private RegistrationController registrationController;
	
	private JTextField textFieldFirstName;
	private JTextField textFieldLastName;
	private JTextField textFieldUsername;
	private JTextField textFieldEmail;
	private JTextField textFieldAge;
	private JTextField textFieldLocation;
	private JPasswordField passwordField_1;
	private JPasswordField passwordField_2;

	/**
	 * Links a new RegistrationDisplay with a RegistrationController
	 * @param c Controller to be linked
	 */
	public RegistrationDisplay(RegistrationController c) {
		super();
		drawRegistration();
		this.registrationController = c;
	}
	
	/**
	 * Links a new RegistrationDisplay with a RegistrationController
	 * and a Display
	 * @param c Controller to be linked
	 * @param lastDisplay Display to be linked
	 */
	public RegistrationDisplay(RegistrationController c, Display lastDisplay) {
		super(lastDisplay.getFrame());
		drawRegistration();
		this.registrationController = c;
	}

	/**
	 * Draws the UI elements and fields for the Registration Window
	 */
	private void drawRegistration() {
		frame.getContentPane().removeAll();
		
		JPanel REGISTRATION = new JPanel();
		REGISTRATION.setBackground(Color.BLACK);
		frame.getContentPane().add(REGISTRATION, "");
		REGISTRATION.setLayout(null);
		
		JLabel lblRegistrationTitle = new JLabel("");
		lblRegistrationTitle.setBounds(77, 7, 737, 69);
		lblRegistrationTitle.setIcon(new ImageIcon(GUI.class.getResource("/images/registration_title.jpg")));
		REGISTRATION.add(lblRegistrationTitle);
		
		JLabel lblFieldsMarkedBy = new JLabel("Fields marked by an asterisk are required.");
		lblFieldsMarkedBy.setBounds(243, 79, 399, 14);
		lblFieldsMarkedBy.setFont(new Font("SansSerif", Font.PLAIN, 10));
		lblFieldsMarkedBy.setForeground(Color.WHITE);
		REGISTRATION.add(lblFieldsMarkedBy);
		
		Component rigidArea_4 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_4.setBounds(57, 142, 20, 20);
		REGISTRATION.add(rigidArea_4);
		
		Component rigidArea_3 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_3.setBounds(77, 142, 20, 20);
		REGISTRATION.add(rigidArea_3);
		
		Component horizontalStrut_17 = Box.createHorizontalStrut(20);
		horizontalStrut_17.setBounds(97, 152, 20, 12);
		REGISTRATION.add(horizontalStrut_17);
		
		Component horizontalStrut_16 = Box.createHorizontalStrut(20);
		horizontalStrut_16.setBounds(117, 152, 20, 12);
		REGISTRATION.add(horizontalStrut_16);
		
		Component horizontalStrut_15 = Box.createHorizontalStrut(20);
		horizontalStrut_15.setBounds(137, 152, 20, 12);
		REGISTRATION.add(horizontalStrut_15);
		
		Component horizontalStrut_14 = Box.createHorizontalStrut(20);
		horizontalStrut_14.setBounds(157, 152, 20, 12);
		REGISTRATION.add(horizontalStrut_14);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(148, 109, 89, 16);
		lblFirstName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblFirstName.setForeground(Color.WHITE);
		REGISTRATION.add(lblFirstName);
		
		JLabel label = new JLabel("*   ");
		label.setBounds(237, 109, 14, 16);
		label.setForeground(Color.RED);
		REGISTRATION.add(label);
		
		textFieldFirstName = new JTextField();
		textFieldFirstName.setBounds(251, 103, 180, 28);
		lblFirstName.setLabelFor(textFieldFirstName);
		REGISTRATION.add(textFieldFirstName);
		textFieldFirstName.setColumns(10);
		
		Component rigidArea_10 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_10.setBounds(468, 107, 20, 20);
		REGISTRATION.add(rigidArea_10);
		
		Component rigidArea_11 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_11.setBounds(495, 107, 20, 20);
		REGISTRATION.add(rigidArea_11);
		
		Component rigidArea_1 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_1.setBounds(522, 107, 20, 20);
		REGISTRATION.add(rigidArea_1);
		
		Component rigidArea_7 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_7.setBounds(542, 107, 20, 20);
		REGISTRATION.add(rigidArea_7);
		
		Component rigidArea_8 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_8.setBounds(562, 107, 20, 20);
		REGISTRATION.add(rigidArea_8);
		
		Component rigidArea_9 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_9.setBounds(582, 107, 20, 20);
		REGISTRATION.add(rigidArea_9);
		
		Component rigidArea_12 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_12.setBounds(602, 107, 20, 20);
		REGISTRATION.add(rigidArea_12);
		
		Component rigidArea_13 = Box.createRigidArea(new Dimension(20, 20));
		rigidArea_13.setBounds(622, 107, 20, 20);
		REGISTRATION.add(rigidArea_13);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(166, 144, 71, 16);
		lblLastName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLastName.setForeground(Color.WHITE);
		REGISTRATION.add(lblLastName);
		
		JLabel label_1 = new JLabel("*   ");
		label_1.setBounds(237, 144, 14, 16);
		label_1.setForeground(Color.RED);
		REGISTRATION.add(label_1);
		
		textFieldLastName = new JTextField();
		textFieldLastName.setBounds(251, 138, 180, 28);
		lblLastName.setLabelFor(textFieldLastName);
		textFieldLastName.setColumns(10);
		REGISTRATION.add(textFieldLastName);
		
		JLabel lblUsername_1 = new JLabel("Username");
		lblUsername_1.setBounds(176, 179, 61, 16);
		lblUsername_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblUsername_1.setForeground(Color.WHITE);
		REGISTRATION.add(lblUsername_1);
		
		JLabel label_2 = new JLabel("*   ");
		label_2.setBounds(237, 179, 14, 16);
		label_2.setForeground(Color.RED);
		REGISTRATION.add(label_2);
		
		textFieldUsername = new JTextField();
		textFieldUsername.setBounds(251, 173, 180, 28);
		textFieldUsername.setColumns(10);
		REGISTRATION.add(textFieldUsername);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(176, 214, 61, 16);
		lblEmail.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEmail.setForeground(Color.WHITE);
		REGISTRATION.add(lblEmail);
		
		JLabel label_3 = new JLabel("*   ");
		label_3.setBounds(237, 214, 14, 16);
		label_3.setForeground(Color.RED);
		REGISTRATION.add(label_3);
		
		textFieldEmail = new JTextField();
		textFieldEmail.setBounds(251, 208, 180, 28);
		textFieldEmail.setColumns(10);
		REGISTRATION.add(textFieldEmail);
		
		JLabel lblPassword_1 = new JLabel("Password");
		lblPassword_1.setBounds(176, 249, 61, 16);
		lblPassword_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPassword_1.setForeground(Color.WHITE);
		REGISTRATION.add(lblPassword_1);
		
		JLabel label_4 = new JLabel("*   ");
		label_4.setBounds(237, 249, 14, 16);
		label_4.setForeground(Color.RED);
		REGISTRATION.add(label_4);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(251, 243, 180, 28);
		REGISTRATION.add(passwordField_1);
		
		JLabel lblConfirmPassword = new JLabel("Confirm Password");
		lblConfirmPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		lblConfirmPassword.setForeground(Color.WHITE);
		lblConfirmPassword.setBounds(116, 284, 121, 16);
		REGISTRATION.add(lblConfirmPassword);
		
		JLabel label_6 = new JLabel("*   ");
		label_6.setForeground(Color.RED);
		label_6.setBounds(237, 284, 14, 16);
		REGISTRATION.add(label_6);
		
		passwordField_2 = new JPasswordField();
		passwordField_2.setBounds(251, 278, 180, 28);
		REGISTRATION.add(passwordField_2);
		
		JLabel lblAge = new JLabel("Age");
		lblAge.setBounds(177, 319, 61, 16);
		lblAge.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAge.setForeground(Color.WHITE);
		REGISTRATION.add(lblAge);
		
		textFieldAge = new JTextField();
		textFieldAge.setBounds(252, 313, 180, 28);
		textFieldAge.setColumns(10);
		REGISTRATION.add(textFieldAge);
		
		JLabel lblGender = new JLabel("Gender");
		lblGender.setBounds(177, 389, 61, 16);
		lblGender.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGender.setForeground(Color.WHITE);
		REGISTRATION.add(lblGender);
				
		final JRadioButton rdbtnMale = new JRadioButton("Male");
		rdbtnMale.setBackground(Color.BLACK);
		rdbtnMale.setForeground(Color.WHITE);
		rdbtnMale.setBounds(267, 385, 78, 24);
		REGISTRATION.add(rdbtnMale);
		
		final JRadioButton rdbtnFemale = new JRadioButton("Female");
		rdbtnFemale.setForeground(Color.WHITE);
		rdbtnFemale.setBackground(Color.BLACK);
		rdbtnFemale.setBounds(347, 385, 78, 24);
		REGISTRATION.add(rdbtnFemale);
		
		ButtonGroup group = new ButtonGroup();
        group.add(rdbtnFemale);
        group.add(rdbtnMale);
		
		JLabel lblLocation = new JLabel("Location");
		lblLocation.setBounds(177, 354, 61, 16);
		lblLocation.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLocation.setForeground(Color.WHITE);
		REGISTRATION.add(lblLocation);
		
		textFieldLocation = new JTextField();
		textFieldLocation.setBounds(252, 348, 180, 28);
		textFieldLocation.setColumns(10);
		REGISTRATION.add(textFieldLocation);
		
		final JLabel lblFillInFields = new JLabel("Please fill in all required fields.");
        lblFillInFields.setHorizontalAlignment(SwingConstants.CENTER);
        lblFillInFields.setFont(new Font("Segoe UI", Font.PLAIN, 13));
        lblFillInFields.setForeground(Color.RED);
        lblFillInFields.setBounds(226, 460, 228, 16);
        lblFillInFields.setVisible(false);
        REGISTRATION.add(lblFillInFields);
		
		JButton button = new JButton("Confirm");
		button.setBounds(252, 424, 85, 28);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Gender gender;
				if (rdbtnFemale.isSelected()) {
					gender = Gender.FEMALE;
				}
				else {
					gender = Gender.MALE;
				}
				
				if (textFieldEmail.getText().equals("") || textFieldUsername.getText().equals("") || textFieldFirstName.getText().equals("") || textFieldLastName.getText().equals("") || textFieldAge.getText().equals("") || textFieldLocation.getText().equals("")
						|| passwordField_1.equals("") || passwordField_2.equals("")) {
					lblFillInFields.setVisible(true);
				}
				else if (!(new String(passwordField_1.getPassword()).equals(new String(passwordField_2.getPassword())))){
					JOptionPane.showMessageDialog(frame, "Password and confirmation do not match.");
				}
				else {
					boolean formattingError = false;
					boolean errorDisplayed = false;
					
					// Alphanumeric/Length test for names.
					if (textFieldFirstName.getText().length() == 0 || textFieldFirstName.getText().length() > 15 || textFieldLastName.getText().length() == 0 || textFieldLastName.getText().length() > 15) {
						formattingError = true;
					}
					
					String temp = textFieldFirstName.getText();
					for (int i = 0; i < temp.length(); i++) {
						if (!('a' <= temp.charAt(i) && temp.charAt(i) <= 'z' || 'A' <= temp.charAt(i) && temp.charAt(i) <= 'Z')) {
							formattingError = true;
						}
					}
					
					temp = textFieldLastName.getText();
					for (int i = 0; i < temp.length(); i++) {
						if (!('a' <= temp.charAt(i) && temp.charAt(i) <= 'z' || 'A' <= temp.charAt(i) && temp.charAt(i) <= 'Z')) {
							formattingError = true;
						}
					}
					
					if (formattingError) {
						JOptionPane.showMessageDialog(frame, "First name and last name must only contain letters, and must be less than 15 characters.");
						formattingError = false;
						errorDisplayed = true;
					}
					
					// Age formatting test.
					if (textFieldAge.getText().length() == 0 || textFieldAge.getText().length() > 2) {
						formattingError = true;
					}
					
					temp = textFieldAge.getText();
					for (int i = 0; i < temp.length(); i++) {
						if (!('0' <= temp.charAt(i) && temp.charAt(i) <= '9')) {
							formattingError = true;
						}
					}
					
					if (formattingError && !errorDisplayed) {
						JOptionPane.showMessageDialog(frame, "Please enter a valid age.");
						formattingError = false;
						errorDisplayed = true;
					}
									
					// Location formatting test.
					if (textFieldLocation.getText().length() == 0 || textFieldLocation.getText().length() > 15) {
						formattingError = true;
					}
					
					temp = textFieldLocation.getText();
					for (int i = 0; i < temp.length(); i++) {
						if (!('a' <= temp.charAt(i) && temp.charAt(i) <= 'z' || 'A' <= temp.charAt(i) && temp.charAt(i) <= 'Z')) {
							formattingError = true;
						}
					}
					
					if (formattingError && !errorDisplayed) {
						JOptionPane.showMessageDialog(frame, "Please enter a valid location.");
						formattingError = false;
						errorDisplayed = true;
					}
					
					if(!errorDisplayed){
						UserSettings newUserSettings = new UserSettings(textFieldEmail.getText(), textFieldFirstName.getText(), textFieldLastName.getText(), gender, Integer.parseInt(textFieldAge.getText()), textFieldLocation.getText());
						lblFillInFields.setVisible(false);
						try {
							registrationController.confirmRegistration(textFieldUsername.getText(), new String(passwordField_1.getPassword()), newUserSettings);
						} catch (RemoteException e1) {
							e1.printStackTrace();
						}
					}
				}
								
			}
		});
		REGISTRATION.add(button);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(347, 424, 85, 28);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				registrationController.returnToLogin();
			}
		});
		REGISTRATION.add(btnCancel);
		
		frame.setVisible(true);
	}
}
